#include "DXUT.h"

#include "UIBase.h"

UIBaseElement::UIBaseElement() {
	m_Texture = NULL;
	m_pEffect = NULL;
	m_IL = NULL;
    m_VB = NULL;
};

UIBaseElement::~UIBaseElement() {

};

void UIBaseElement::OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime ) {
	m_pEffect->GetVariableByName("g_Texture")->AsShaderResource()->SetResource(m_Texture);
	m_pEffect->GetTechniqueByName("RenderUI")->GetPassByIndex(0)->Apply(0, pd3dImmediateContext);

		UINT stride = sizeof( UIVertex );
		UINT offset = 0;

		// Set the input layout
		pd3dImmediateContext->IASetInputLayout( m_IL );
		pd3dImmediateContext->IASetVertexBuffers( 0, 1, &m_VB, &stride, &offset );
		pd3dImmediateContext->Draw( 6, 0 );

	m_pEffect->GetTechniqueByName("RestoreState")->GetPassByIndex(0)->Apply(0, pd3dImmediateContext);
};

void UIBaseElement::OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc ) {
	WCHAR path[MAX_PATH];
    ID3DXBuffer* pShader = NULL;
	DXUTFindDXSDKMediaFileCch(path, MAX_PATH, L"HUDShader.fx");
    D3DXCompileShaderFromFile(path, NULL, NULL, NULL, "fx_5_0", 0, &pShader, NULL, NULL);
    D3DX11CreateEffectFromMemory(pShader->GetBufferPointer(), pShader->GetBufferSize(), 0, pd3dDevice, &m_pEffect);
	SAFE_RELEASE(pShader);

	D3DX11CreateShaderResourceViewFromFile( pd3dDevice, L"terrain.png", NULL, NULL, &m_Texture, NULL );
	ID3DX11EffectShaderResourceVariable* tex_variable;
	tex_variable = m_pEffect->GetVariableByName("g_Texture")->AsShaderResource();
	tex_variable->SetResource(m_Texture);

	D3D11_INPUT_ELEMENT_DESC layout[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,  0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,    0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	};
	D3DX11_PASS_DESC passDesc;
    m_pEffect->GetTechniqueByName("RenderUI")->GetPassByIndex(0)->GetDesc(&passDesc);
    pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &m_IL);


	UIVertex vertices[] =
	{
		{ D3DXVECTOR3( -100.0f, -100.0f, 0.8f ), D3DXVECTOR2( 0.0f, 0.0f )}, 
		{ D3DXVECTOR3( 100.0f, 100.0f, 0.8f ), D3DXVECTOR2( 1.0f, 1.0f )},
		{ D3DXVECTOR3( -100.0f, 100.0f, 0.8f ), D3DXVECTOR2( 0.0f, 1.0f )},

		{ D3DXVECTOR3(-100.0f, -100.0f, 0.8f ), D3DXVECTOR2( 0.0f, 0.0f )},
		{ D3DXVECTOR3( 100.0f, -100.0f, 0.8f ), D3DXVECTOR2( 1.0f, 0.0f )},
		{ D3DXVECTOR3( 100.0f, 100.0f, 0.8f ), D3DXVECTOR2( 1.0f, 1.0f )},		
	};
	D3D11_BUFFER_DESC bd;
	ZeroMemory( &bd, sizeof(bd) );
	bd.Usage = D3D11_USAGE_DEFAULT;
	bd.ByteWidth = sizeof( UIVertex ) * 6;
	bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = 0;
	D3D11_SUBRESOURCE_DATA InitData;
	ZeroMemory( &InitData, sizeof(InitData) );
	InitData.pSysMem = vertices;
	pd3dDevice->CreateBuffer( &bd, &InitData, &m_VB );
};

void UIBaseElement::OnD3D11DestroyDevice( ) {
	SAFE_RELEASE( m_Texture );
	SAFE_RELEASE( m_pEffect );
	SAFE_RELEASE( m_IL );
	SAFE_RELEASE( m_VB );
};


void UIBaseElement::OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc ) {

};

void UIBaseElement::OnD3D11ReleasingSwapChain( ) {

};


void UIBaseElement::handleMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos ) {

};

void UIBaseElement::handleKeyboard( UINT nChar, bool bKeyDown, bool bAltDown ) {

};
